import sys
from untils.api import *
import requests, random, os
from io import BytesIO
import win32clipboard
from PIL import Image
# 你申请的key
key = 'xxx'


def Message(question):
    default = '你好！我是机器人小易\n   我可以为您提供：聊天、讲笑话、解梦、查询星座运势、看小姐姐（黑丝/美女小姐姐）、英文翻译、讲笑话 等服务喔~\n' \
              '快来体验吧！\n\n使用说明:回复：讲笑话，可为你讲笑话 \n回复：解梦：xx，可为您解梦\n回复：xxx星座运势即可为您查询 星座运势\n回复：讲笑话，即可为您讲笑话,' \
              '\n回复：看美女，即可为您发送美女照片,回复：黑丝美女即可为您发送黑丝美女\n回复：翻译：xxxx，例：翻译：aborad,即可为您翻译'
    xz = '白羊座运势、金牛座运势、双子座运势、巨蟹座运势、狮子座运势、处女座运势、天秤座运势、天蝎座运势、射手座运势、摩羯座运势、水瓶座运势、双鱼座运势。'.split('、')
    if question == '小姐姐' or question == '看美女':
        path = 'imgs/'
        files = os.listdir(path)
        img = path + random.choice(files)
        print(img)
        setimg(img)
        return '看美女'
    elif question == '关机':
        sys.exit()
    elif question == '你会什么' or question == 'help' or question == '帮助':
        return default
    elif question == '讲笑话' or question == '笑话' or '笑话' in question:
        result = xh()
        return result
    elif question == '黑丝美女':
        path = 'hs/'
        files = os.listdir(path)
        img = path + random.choice(files)
        print(img)
        setimg(img)
        return '看美女'
    elif '翻译' in question:
        result = translate(question)
        return result
    elif question in xz:
        question = question.replace('运势', '')
        result = constellation(question)
        return result
    elif '解梦：' in question:
        question = question.replace('解梦：', '')
        result = oneiromancy(question)
        return result
    else:
        # return robotmsg(question)
        return msg(question)


def msg(content):
    url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg={}'.format(content)
    response = requests.get(url)
    message = response.json()['content']
    return message


def robotmsg(msg):
    url = f'http://api.tianapi.com/robot/index?key={key}&question={msg}&mode=0&priv=0'
    response = requests.get(url)
    data = response.json()['newslist'][0]['reply']
    if '{robotname}' in data:
        data = str(data).replace('{robotname}', '')
    return data


def send_to_clipboard(clip_type, data):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(clip_type, data)
    win32clipboard.CloseClipboard()


def setimg(img):
    image = Image.open(img)
    output = BytesIO()
    image.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()
    send_to_clipboard(win32clipboard.CF_DIB, data)


if __name__ == '__main__':
    pass
# msg = Message('早安')
# print(msg)
# setimg('config/newmsg.png')

# msg('你好')
